var mysql = require('mysql'); //调用MySQL模块
var config = require('./config');

module.exports = function (sqls) {
    var fn = fn || '';
    return new Promise((resolve, reject) => {
        //创建一个connection
        var connection = mysql.createConnection(config);
        var data = '';

        
        //创建一个connection
        connection.connect(function (err) {
            if (err) {
                console.log('[query] - :' + err);
                return;
            }
            console.log('[connection connect]  succeed!');
        });

        //执行SQL语句
        connection.query(sqls, function (err, rows, fields) {
            if (err) {
                console.log('[query] - :' + err);
                resolve({
                    erro : '',
                    status : 1
                });
                return;
            }else {
                resolve(rows);
            }
            console.log('The solution is: ', rows);
        });

        //关闭connection
        connection.end(function (err) {
            if (err) {
                return;
            }
            console.log('[connection end] succeed!');
        });
    });
}